var app = getApp();
Page(Object.assign({},{
    data:{
        page : {
            isend : false,
            doing : false,
            doo : 'kefu',
            pdata : {
                op : 'list',
                page : 1,
                status : 0,
            },
            waitf : 0,
            nodataf : 0,
        },
        datalist : [],
        inited : false,
        scrolltop : 99999,
        timer : null,
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
    },

    onLoad:function(options){
        var self = this;

        var rpx = 750/app.globalData.sysInfo.windowWidth;
        var height = rpx*app.globalData.sysInfo.windowHeight - 20;
        var shopid = options.shopid ? options.shopid : 0;
        var uid = options.uid ? options.uid : 0;
        var gid = options.gid ? options.gid : 0;
        var jid = options.jid ? options.jid : 0; // 集字
        var role = options.role ? options.role : '';
        self.setData({
            'page.pdata.shopid' : shopid,
            'page.pdata.uid' : uid,
            'page.pdata.gid' : gid,
            'page.pdata.jid' : jid,
            'page.pdata.role' : role,
            msgheight : height,
        });
        app.com.auth(function(user){
            app.com.getPage(self,self.data.page,20,function(res){
                var data = res.data.data;
                self.setData({
                   datalist : data.list,
                   Pagetheme : data.params.style,
                   params : data.params,
                   info : data.info,
                   goods : data.goods ? data.goods : null,
                   jizi : data.jizi ? data.jizi : null,
                },function(){
                    self.setData({
                        inited : true,
                    });
                    self.getNewMess();
                    wx.setNavigationBarTitle({
                        title : self.data.info.title,
                    });
                });

            },false,function(res){
                app.com.alert(res.data.message,function(){
                    app.com.backPage(0);
                });
            });
        });

        this.data.sendau = wx.createAudioContext("sendau");
        this.data.getau = wx.createAudioContext("getau");

    },
    onUnload(){
        var self = this;
        clearInterval(this.data.timer);
        var postdata = {
            op : 'leaveout',
            shopid : self.data.page.pdata.shopid,
            uid : self.data.page.pdata.uid,
            role : self.data.page.pdata.role,
        };
        app.com.http('kefu','POST',postdata,0,false,'','',function(res){});
    },
    onHide(){
        clearInterval(this.data.timer);
    },
    onShow(){
        if( this.data.inited ){
            this.getNewMess();
        }
    },
    getNewMess(){
        var self = this;
        self.data.timer = setInterval(function(){
            var postdata = {
                op:'newmess',
                shopid : self.data.page.pdata.shopid,
                uid : self.data.page.pdata.uid,
                role : self.data.page.pdata.role,
            };
            var length = self.data.datalist.length;
            var maxid = 0;
            for (var i = 0; i < self.data.datalist.length; i++) {               
                if( self.data.datalist[i].id*1 > maxid*1 ){
                    
                    maxid = self.data.datalist[i].id;
                }
            }
            postdata.maxid = maxid;
            
            if( self.data.isnew ) return false;
            self.data.isnew = true;

            app.com.http('kefu','POST',postdata,0,false,'','',function(res){
                if (res.data && !res.data.errno) {
                    var isin = 0;
                    for (var i = 0; i < res.data.data.new.length; i++) {
                        var item = res.data.data.new[i];
                        for (var j = 0; j < self.data.datalist.length; j++) {
                            if( self.data.datalist[j].id == item.id ){
                                res.data.data.new.splice(i,1);
                            }
                        }
                    }
                    self.setData({
                        datalist : self.data.datalist.concat(res.data.data.new),
                        scrolltop : self.data.scrolltop + 999,
                    });
                    self.data.getau.play();
                }
                self.data.isnew = false;
            });
        },3000)
    },
    sendMsgBtnFun(){
        this.sendMsg(0);
    },
    sendCommodityCard(e){
        var type = e.currentTarget.dataset.type;
        this.sendMsg(type);
    },
    sendMsg(type){
        var self = this;
        var type = type ? type : 0;
        if( !self.data.msg && type == 0 ) {
            app.com.toast('请填写发送的内容');
            return false;
        }
        var postdata = {
            op:'submess',
            content : self.data.msg ? self.data.msg : '',
            img : self.data.img ? self.data.img : '',
            shopid : self.data.page.pdata.shopid,
            uid : self.data.page.pdata.uid,
            gid : self.data.page.pdata.gid,
            jid : self.data.page.pdata.jid,
            role : self.data.page.pdata.role,
            type : type,
        };

        app.com.http('kefu','POST',postdata,0,true,'','',function(res){
            if (res.data && !res.data.errno) {
                self.setData({
                    datalist : self.data.datalist.concat(res.data.data.new),
                    scrolltop : self.data.scrolltop + 999,
                    msg : '',
                    img : '',
                });
                if( type == 2 ){
                    self.setData({
                        goods : null,
                    });
                }
                self.data.sendau.play();
            }else{
                app.com.alert(res.data.message);
            }
        });
        self.subMess();
    },
    handleHistoryMsg(){
        var self = this;
        self.setData({
            moreing : true
        });
        app.com.getPage(self,self.data.page,0,function(res){
            self.setData({
                datalist : res.data.data.list.concat(self.data.datalist),
                moreing : false,
            });
        });
    },
    toolbarFun(){
        var self = this;
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success (res) {
                var  src = res.tempFilePaths[0];
                
                wx.showLoading({mask:true,title:'上传中'});
                wx.uploadFile({
                    url: app.siteInfo.siteroot+'?i='+app.siteInfo.uniacid+'&j=&c=utility&a=file&do=upload&type=image&thumb=0',
                    filePath: src,
                    name: 'file',
                    success: function(res){
                        var aaa = JSON.parse( res.data );
                        if( aaa.attachment && aaa.url ){

                            self.setData({
                                img : aaa.attachment,
                            },function(){
                                self.sendMsg(1);
                            });

                        }else{
                            app.com.toast('上传图片失败');
                        }
                    },
                    complete : function(){
                        wx.hideLoading();
                    }
                })


            }
        })
    },
    bindChatField(e){
        this.setData({
            msg : e.detail.value
        })
    },
    bindFocus(e){
        this.setData({
            inputing : true,
        })
    },
    bindBlur(e){
        this.setData({
            inputing : false,
        })
    },
    imageShowOrHideFun(e){
        var img = e.currentTarget.dataset.src;
        wx.previewImage({
            current: img,
            urls: [img]
        })
    },
    togglece(e){
        this.setData({
            showconfirm : false
        })
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e,this.data.tid );
    },
    redirectto : function( e ){
        app.com.redirectto( e,this.data.tid );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    },
    FormIdsubmit : function(e){
        if( app.globalData.formarr.length <= 20 ) app.globalData.formarr.push(e.detail.formId);
    },
    subMess(){
        var self = this;
        wx.requestSubscribeMessage({
            tmplIds : self.data.params.submess,
            complete : function(){}
        })        
    }

}))